package com.seu.me.thread.day5.concurrent017;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

class Temp extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName() + " run, now = " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class ScheduledJob {

    public static void main(String args[]) throws Exception {

        Temp command = new Temp();
        /**
         * 需要注意的是线程池中的线程是可以重用的，而 new thread 什么的，用完就没了。
         * 1个当然有用了，可以限制只有一个线程进行工作。
         */
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        ScheduledFuture<?> scheduleTask = scheduler.scheduleWithFixedDelay(command, 5, 2, TimeUnit.SECONDS);

    }
}